/**
 * 
 */
package com.java.controller.front;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.entity.Function;
import com.java.entity.Users;
import com.java.mapper.UsersMapper;
import com.java.service.impl.FunctionServiceImpl;
import com.java.service.impl.UserServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
* @author Kidney
* 创建时间：2019年4月23日 下午3:54:13
* Description:
*/
/**
 * @author KIDNEY
 *
 */
@Controller
public class TestController {

	@Autowired
	private UserServiceImpl us;
	
	@Autowired
	private FunctionServiceImpl fs;

	@RequestMapping(value = "test")
	@ResponseBody
	public String test() {

		List<Function> functions = fs.findByUserId(1);
		JSONArray json = JSONArray.fromObject(functions);

		return json.toString();
	}


	public void loginByRealm() throws Exception {

		// 读取ini数据源文件，获取securityManager工厂
		Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
				"classpath:shiro-realm.ini");
		// 2、得到SecurityManager实例并绑定给SecurityUtils
		org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();

		SecurityUtils.setSecurityManager(securityManager);

		// 3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("shen", "666");
		try {
			// 4、登录，即身份验证
			subject.login(token);
		} catch (AuthorizationException e) {
			// 5、身份验证失败
			e.printStackTrace();
		}
		System.out.println(subject.isAuthenticated());
		subject.logout();
		System.out.println(subject.isAuthenticated());
	}

	
	

	public void testMd5() {
		// md5加密算法测试
		String password = "123";
		String salt = "siyuan";
		int times = 1;
		Md5Hash md = new Md5Hash(password, salt, times);
		System.out.println(md.toString());

		SimpleHash sh = new SimpleHash("sha", password, salt, times);
		System.out.println(sh.toString());
	}
	

	
	
	
	
}
